package com.bishe.app.modules.userrole.controller;

import com.bishe.app.core.api.CommonResult;
import com.bishe.app.core.api.CommonPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.app.core.enums.IsDel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bishe.app.modules.userrole.service.UserRoleService;
import com.bishe.app.modules.userrole.entity.UserRole;

import java.util.List;

import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 角色-用户表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-13
 */
@RestController
@RequestMapping("/userRole")
public class UserRoleController {

    @Autowired
    public UserRoleService userRoleService;

    @RequestMapping(value = "/delUserRole", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(Long roleId, Long userId) {
        userRoleService.lambdaUpdate()
                .eq(UserRole::getRoleId, roleId)
                .eq(UserRole::getUserId, userId)
                .set(UserRole::getIsDel, IsDel.Y.getCode())
                .update();
        return CommonResult.success(null);
    }


    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody UserRole userRole) {
        boolean success = userRoleService.save(userRole);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody UserRole userRole) {
        userRole.setId(id);
        boolean success = userRoleService.updateById(userRole);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = userRoleService.removeById(id);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean success = userRoleService.removeByIds(ids);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }


    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UserRole>> listAll() {
        List<UserRole> userRoleList = userRoleService.list();
        return CommonResult.success(userRoleList);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<UserRole>> page(@RequestBody CommonPage commonPage) {
        Page<UserRole> page = new Page(commonPage.getPageNum(), commonPage.getPageSize());
        return CommonResult.success(CommonPage.restPage(userRoleService.page(page)));
    }
}



